package org.eiahe.hr.training.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.eiahe.hr.training.domain.bo.HrTrainingRecordBo;
import org.eiahe.hr.training.domain.vo.HrTrainingRecordVo;
import org.eiahe.hr.training.service.IHrTrainingRecordService;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 培训记录Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/training/record")
public class HrTrainingRecordController extends BaseController {

    private final IHrTrainingRecordService recordService;

    /**
     * 查询培训记录列表
     */
    @SaCheckPermission("hrTraining:record:list")
    @GetMapping("/list")
    public TableDataInfo<HrTrainingRecordVo> list(HrTrainingRecordBo bo, PageQuery pageQuery) {
        return recordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出培训记录列表
     */
    @SaCheckPermission("hrTraining:record:export")
    @Log(title = "培训记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HrTrainingRecordBo bo, HttpServletResponse response) {
        List<HrTrainingRecordVo> list = recordService.exportTrainingRecord(bo);
        ExcelUtil.exportExcel(list, "培训记录", HrTrainingRecordVo.class, response);
    }

    /**
     * 获取培训记录详细信息
     *
     * @param recordId 培训记录主键
     */
    @SaCheckPermission("hrTraining:record:query")
    @GetMapping("/{recordId}")
    public R<HrTrainingRecordVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long recordId) {
        return R.ok(recordService.queryById(recordId));
    }

    /**
     * 新增培训记录
     */
    @SaCheckPermission("hrTraining:record:add")
    @Log(title = "新增培训记录", businessType = BusinessType.INSERT,linkType = "training",nameField = "employeeName")
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HrTrainingRecordBo bo) {
        return toAjax(recordService.insertByBo(bo));
    }

    /**
     * 修改培训记录
     */
    @SaCheckPermission("hrTraining:record:edit")
    @Log(title = "培训记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrTrainingRecordBo bo) {
        return toAjax(recordService.updateByBo(bo));
    }

    /**
     * 删除培训记录
     *
     * @param recordIds 培训记录主键串
     */
    @SaCheckPermission("hrTraining:record:remove")
    @Log(title = "培训记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] recordIds) {
        return toAjax(recordService.deleteByIds(Arrays.asList(recordIds)));
    }

    /**
     * 批量导入培训记录
     */
    @SaCheckPermission("hrTraining:record:import")
    @Log(title = "培训记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<String> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        List<HrTrainingRecordVo> list = ExcelUtil.importExcel(file.getInputStream(), HrTrainingRecordVo.class);
        String message = recordService.importTrainingRecord(list, updateSupport);
        return R.ok(message);
    }

    /**
     * 下载培训记录导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(null, "培训记录", HrTrainingRecordVo.class, response);
    }

    /**
     * 完成学习
     */
    @SaCheckPermission("hrTraining:record:complete")
    @GetMapping("/complete/{courseId}")
    public R<Void> complete(@NotNull(message = "主键不能为空") @PathVariable Long courseId) {
        return toAjax(recordService.completeCourseDetail(courseId));
    }
}
